<?php

namespace App\Http\Controllers\Api\V1;

use Carbon\Carbon;
use Carbon\CarbonPeriod;
use App\Models\Brokerage;
use App\Services\TakeBrokerageToBalance\TakeBrokerageToBalance;
use Illuminate\Support\Facades\Cache;
use App\Services\Cache\BrokerageCache;
use App\Http\Controllers\Api\V1\Resources\BrokerageResource;
use Illuminate\Database\Eloquent\Builder;


class BrokeragesController extends Controller
{
	public function index()
	{
		$user = request()->user();
		$query = Brokerage::query()->where('user_id', $user->id);
		$query = !empty(request('start_time')) ? $query->where('created_at', '>', request('start_time')) : $query;
		$query = !empty(request('end_time')) ? $query->where('created_at', '<', request('end_time')) : $query;
		$brokerages = $query->where('is_taken',true)->where('remark','pay_order')->orderByDesc('id')->paginate();
		

		return BrokerageResource::collection($brokerages);
	}



	public function packets()
	{
		$user = request()->user();

		$time = request('time');
		if(!$time){
			fail('请选择时间');

		}
		$query = Brokerage::query()->where('user_id', $user->id);
		$query = !empty(request('time')) ? $query->whereDate('created_at', request('time')) : $query;

		$brokerages = $query->orderByDesc('id')->paginate();
		

		return BrokerageResource::collection($brokerages);
	}

	public function times()
	{
		$user = request()->user();

		$datePperiod = new CarbonPeriod(Carbon::today()->subDays(10), Carbon::today());
		$data = [];
		foreach ($datePperiod as $k => $value) {
		    $data[$k]['time'] = $value->format('Y-m-d');
		    $data[$k]['can_be_taken'] = (bool)$user->brokerages()->whereDate('created_at',$value->format('Y-m-d'))->where("is_taken",false)->first();
		}
		$data = array_reverse($data);

		return ['data' => $data];

	}

	public function info()
	{
		$user = request()->user();

		$time = request('time');
		if(!$time){
			fail('请选择时间');

		}
		$query = Brokerage::query()->where('user_id', $user->id);

		$query = !empty(request('time')) ? $query->whereDate('created_at', request('time')) : $query;

		$payMoney = $query->sum('pay_money');
		$money = $query->where('is_taken',true)->sum('money');


		$resQuery = Brokerage::query()->where('user_id', $user->id);
		$resQuery = !empty(request('time')) ? $resQuery->whereDate('created_at', request('time')) : $resQuery;

		$canBeTaken = (bool)$resQuery->where("is_taken",false)->first();

		$takeTime = (string)Carbon::parse($time)->addDays(1)->addHours(22);
		
		/*$diffTime = "";
		if($takeTime->gt(Carbon::now())){
			//
			$diffTime = $takeTime->diffInSeconds(Carbon::now(),true);

			$day = floor($diffTime / 86400);
			
			$diffTime = Carbon::parse($diffTime)->format('H:i:s');
			
			if($day>0){

				$explodeArr = explode(":",$diffTime);
				$explodeArr[0] = $day*24 + $explodeArr[0];
				$diffTime = implode(":",$explodeArr);

			}
		}*/


		return ['money' => $money, 'pay_money'=> $payMoney,'can_be_taken' => $canBeTaken,'can_take_time' => $takeTime];
	}

	public function take(Brokerage $brokerage)
	{

		$lock = Cache::lock('brokerage_take', 1);
    	
    	try {

    		if (! $lock->get()) fail('太快了，请再试一次');
    		if($brokerage->is_taken) fail('红包已领取过');
    		$time = allow_time(Carbon::parse($brokerage->created_at)->format("Y-m-d"));
    		
    		if(Carbon::parse($time)->gt(Carbon::now())){

    			fail('未到领取时间');
    		}
    		
    		$user = request()->user();

		    app(TakeBrokerageToBalance::class, [
			    'user' => $user,
			    'brokerage' => $brokerage
		    ])->apply();

		    
	    } finally {
    		$lock->release();
	    }
	    
	    return response(null, 204);


	}

	public function count()
	{
		$count = app(BrokerageCache::class)->count();
		return response()->json($count);
	}
}
